package com.hjt.gulimall.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.hjt.common.exception.ErrorCodeEnum;
import com.hjt.common.to.member.UserLoginTo;
import com.hjt.gulimall.member.exception.EmailHasExistedException;
import com.hjt.gulimall.member.exception.UserNameHasExistedException;
import com.hjt.gulimall.member.feign.CouponFeignService;
import com.hjt.gulimall.member.vo.RegisterVo;
import com.hjt.gulimall.member.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.hjt.gulimall.member.entity.MemberEntity;
import com.hjt.gulimall.member.service.MemberService;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.R;



/**
 * 会员
 *
 * @author huangjingtai
 * @email 970075985@qq.com
 * @date 2021-09-08 19:39:20
 */
@RestController
@RequestMapping("member/member")
@RefreshScope
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @Value("${member.user.name}")
    private String name;

    @Value(("${member.user.age}"))
    private String age;

    @RequestMapping("/config")
    public R testConfig() {
        return R.ok().put("name",name).put("age", age);
    }

    @RequestMapping("/test")
    public R test() {
        Map<String, Object> query = new HashMap<>();
        return R.ok().put("coupons", couponFeignService.list(query));
    }


    // 用户注册
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo) {
        try {
            memberService.register(registerVo);
            return R.ok();
        } catch (UserNameHasExistedException e) {
            return R.error(ErrorCodeEnum.USER_NAME_HAS_EXISTED_EXCEPTION.getCode(),ErrorCodeEnum.USER_NAME_HAS_EXISTED_EXCEPTION.getMessage());
        } catch (EmailHasExistedException e) {
            return R.error(ErrorCodeEnum.EMAIL_HAS_REGISTERED_EXCEPTION.getCode(),ErrorCodeEnum.EMAIL_HAS_REGISTERED_EXCEPTION.getMessage());
        }
    }

    // 登录
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo loginVo) {
        UserLoginTo loginTo = memberService.login(loginVo);
        if (loginTo != null) {
            return R.ok().put("data",loginTo);
        } else {
            return R.error(ErrorCodeEnum.USERACCOUNT_PASSWD_INVALID_EXCEPTION.getCode(),ErrorCodeEnum.USERACCOUNT_PASSWD_INVALID_EXCEPTION.getMessage());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
